/**  
 *	VisualGroup JavaScript prototype Classes, version 0.1
 *  (c) 2008 Bill Vison <vison_ruan@126.com>
 *
 *
/*--------------------------------------------------------------------------*/
using("Vsg.FlashRemoteHandler");
using("Vsg.FlashHttpRequest");

Vsg.FlashRemoteHandler.Load  = function(form, options){

	Vsg.FlashRemoteHandler.Load.superclass.constructor.call(this, form, options);
	this.reader = this.form.reader;
};


Ext.extend(Vsg.FlashRemoteHandler.Load , Ext.form.Action , {
	type:"flashload",
	
	run : function() {

		var $sendObj = this.createCallback(this.options);

		Ext.apply($sendObj, {
                    method:this.getMethod(),
                    url:this.getUrl(false),
                    headers: this.options.headers,
                    params:this.options.params || {}
        });
		
		try {
			Vsg.FlashHttpRequest.request($sendObj);
		} catch (ex) {
			Ext.log(ex);
		}
		
	},
	
	getMethod : function(){
        return (this.options.method || 'POST').toUpperCase();
    },

    getUrl : function(appendParams){
        var url = this.options.url || this.form.url;
        if(appendParams){
            var p = this.getParams();
            if(p){
                url += (url.indexOf('?') != -1 ? '&' : '?') + p;
            }
        }
        return url;
    },

	success : function(response) {
		var result = this.processResponse(response);

        if(result === true || !result.success || !result.data){
            this.form.afterAction(this, false);
            return;
        }
        this.form.clearInvalid();
        this.form.setValues(result.data);
        this.form.afterAction(this, true);		
	},
	
	handleResponse : function(response) {

		if(this.form.reader){
            var rs = this.form.reader.read(response);
            var data = rs.records && rs.records[0] ? rs.records[0].data : null;
            return {
                success : rs.success,
                data : data
            };
        }

        return Ext.decode(response.responseText);
	}

});
